# --------------------------------------------------------
# ' Figure 1. political advertisement by close election status
# --------------------------------------------------------
library(here)
library(rio)
library(data.table)
library(maps)
library(stringr)
library(ggpubr)
library(ggsci)
library(viridis)
library(geofacet)

# specify your root directory here
here = '~/Dropbox/project-archive/BG-bk/dataverse'

source(file.path(here,'code','rdata_utils.R'))

# read data 
ad_year = data.table(import(file.path(here,'data','processed','political_ad_year.dta')))

# process close election data
state.election = read.csv(file.path(here,'data','rawdata',"state_election_all.csv"),stringsAsFactors=FALSE)

state.full <- state.election[,1]
state.abb <- state.election[,2]

b <- strsplit(names(state.election),"_")
year <- unlist(lapply(b,function(x) x[2]))
year[1] <- NA 

c <- unlist(lapply(b,function(x) x[1]))
d <-strsplit(c,"[.]")

democratic <- unlist(lapply(d,function(x) sum(x %in% "Democratic")))
republican <- unlist(lapply(d,function(x) sum(x %in% "Republican")))
category <- unlist(lapply(d,function(x) x[length(x)]))

list.year <- unique(year) 
list.year <- list.year[2:length(list.year)]

list.data <- list()
list.year <- c("2016","2012","2008","2004","2000","1996","1992","1988")

for (yy  in list.year){

	yy.data <- state.election[,grep(yy,names(state.election))] 
	
	out.data <- array(NA,dim=c(length(state.full),4))
	total <- yy.data[,grep("Total",names(yy.data))]
	rep <- yy.data[,grep("Republican",names(yy.data))]
	dem <- yy.data[,grep("Democratic",names(yy.data))]
	total.d <- rep+dem 
	p.rep <- rep / total.d 
	p.dem <- dem / total.d

	out.data[,1] <- as.numeric(yy)
	out.data[,2] <- rep 
	out.data[,3] <- dem 
	out.data[,4] <- total
	list.data[[match(yy, list.year)]] <- out.data
}

state_close <- as.data.frame(do.call(rbind,list.data))
state_close$state <- rep(state.abb,8)

colnames(state_close) <- c("year","rep","dem","total",'state')

# ---- calculate election closeness
state_close$abs_diff <- abs(state_close$rep-state_close$dem)/abs(state_close$rep+state_close$dem)
state_close$close <- ifelse(state_close$abs_diff<0.05,1,0)

state_close = as.data.table(state_close)

state_fips = data.table(state.fips)
state_fips[, fips:=str_pad(fips, 2, pad = "0")]
state_close = merge(x = state_close, y = unique(state_fips[,c("abb","fips")]), by.x = "state", by.y="abb",all.x=TRUE)

# ---- merge two data
ad_close = merge(x=ad_year,y=state_close,by=c('state','year'),all.x=TRUE)

ad_close = ad_close[state!='',]
ad_close = ad_close[state!='US',]

ad_close[,close_election := 0]
ad_close[0.05 <= abs_diff & abs_diff < 0.10,close_election := 1]
ad_close[0.00 <= abs_diff & abs_diff < 0.05,close_election := 2]
ad_close[,close_election_group := factor(close_election,levels=c(0,1,2),
	labels=c('winning margin > 10%','5% ~ 10%','5% below'))]

ad_close[, p_tone_attack := sum_tone_attack/(sum_tone_attack+sum_tone_promote+sum_tone_contrast)]
ad_close[, p_tone_contrast := sum_tone_contrast/(sum_tone_attack+sum_tone_promote+sum_tone_contrast)]
ad_close[, p_tone_promote := sum_tone_promote/(sum_tone_attack+sum_tone_promote+sum_tone_contrast)]
ad_close[,mean_centered_n_ad := n_ad - mean(n_ad), by='year']

options(scipen=10000)

#------------------------------------------------------------------------------
# Figure 1
#==============================================================================
p = ad_close %>% ggplot(aes(x=as.factor(year),y=mean_centered_n_ad,fill=close_election_group)) + 
	scale_fill_manual(values = c('winning margin > 10%'='gray','5% ~ 10%'='pink','5% below'='red'))+
	geom_bar(stat='summary',fun.y = "mean",position=position_dodge()) +
	theme_pubr() +
	guides(fill = guide_legend(title = ""))+
	facet_wrap(~variable,scale='free') + 
	facet_geo(~ state,grid = "us_state_grid2", label = "name") + 
	labs(y='Total Number of Political Advertisement - Mean', x='') +
	theme(axis.text=element_text(size=6))+
	theme(strip.text.x = element_text(size = 6))
ggsave(file=file.path(here,'output','figure_geo_n_ad.png'), plot=p, width=11, height=7,dpi=1000)


#------------------------------------------------------------------------------
# Figure S1
#==============================================================================

long_ad_close = melt(ad_close[,c('year','state','close_election_group','p_tone_attack','p_tone_contrast','p_tone_promote')],
	id.vars=c('year','state','close_election_group'))
long_ad_close[,variable := factor(variable,levels=c('p_tone_attack','p_tone_contrast','p_tone_promote'),
	labels=c('Attack tone','Contrast tone','Promote tone'))]
p = long_ad_close %>% ggplot(aes(x=as.factor(year),y=value,fill=close_election_group)) + 
	geom_boxplot()+
	scale_fill_manual(values = c('winning margin > 10%'='gray','5% ~ 10%'='pink','5% below'='red'))+
	theme_pubr() +
	guides(fill = guide_legend(title = "")) +
	facet_wrap(~variable) + 
	labs(y='Proportion of Political Advertisement', x='')
ggsave(file=file.path(here,'output','figure_box_tone.png'), plot=p, width=7, height=5, dpi=1000)


